/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginJFrame.java
 *
 * Created on Apr 18, 2009, 6:57:18 PM
 */

package br.com.janalise.view;

import br.com.janalise.system.Configurar;
import br.com.janalise.system.ManagerFiles;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import br.com.janalise.leitorpdf.Leitor;

/**
 *
 * @author bosco
 */
public class SplashJframe extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;

     private Image mainicon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/main.png"));
     private Image question = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/question.png"));
     public JLabel log;
     public JProgressBar barFile = new JProgressBar();
     public JProgressBar barLeitor = new JProgressBar();

     public void init(){
            Object opcao[] = {"Sim","Não"};
            int operacao = JOptionPane.showOptionDialog(null, "Deseje definir o repositório?", "Repositório", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,new ImageIcon(question),opcao,opcao[1]);
            if (operacao == JOptionPane.YES_OPTION) {
                JFileChooser jfc = new JFileChooser(System.getProperty("user.home"));
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                jfc.showOpenDialog(null);
                File dir = jfc.getSelectedFile();
                try {
                    Configurar.getInstance().setRepositorio(dir!=null?dir.getAbsolutePath():null);
                    if(dir!=null)
                        JOptionPane.showMessageDialog(null, "repositório modifcado com sucesso!\n" + dir.getAbsolutePath(), "Repositório", JOptionPane.INFORMATION_MESSAGE);
                    Configurar.getInstance().getPathRepositorio();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    }

    /** Creates new form LoginJFrame */
    public SplashJframe() throws InterruptedException {
        init();
        initComponents();
        this.setVisible(true);
        log = new JLabel("Iniciando leitura...");
        log.setHorizontalAlignment(JLabel.CENTER);
        log.setForeground(Color.WHITE);
        log.setFont(new Font(Font.DIALOG, Font.BOLD, 12));

        jLabel1.setLayout(null);        
        barFile.setBounds(100,310, 300, 10);
        barLeitor.setBounds(100,325, 300, 10);

        log.setBounds(0, 340, this.getX(), 20);

        jLabel1.add(barFile);
        jLabel1.add(log);

        log.setText("Leitura iniciada...");
        Configurar config = Configurar.getInstance();
        log.setText("Carregando Configuração...");
        try {
            config.setPathRepositorio(config.getRepositorio());
            List<File> filelogs = ManagerFiles.getInstance().getFiles();
            barFile.setMaximum(filelogs.size());
            for (File file : filelogs) {
                log.setText("[File]- "+file.getName());
                log.setText("Iniciando leitura dos arquivos");
                barFile.setValue(barFile.getValue()+1);

//                new ExtractTextInRectangle(file.getAbsolutePath(),this,true);
                new Leitor(file,this).run();
            }
            log.setText("Repositório carregado...");
        } catch (IOException ex) {
            Logger.getLogger(SplashJframe.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Splash");
        setAlwaysOnTop(true);
        setIconImage(mainicon);
        setUndecorated(true);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/splash.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
     *        
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
